﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using SERP.DAO;
using SERP.Controllers;
using System.Web.UI.WebControls;
namespace SERP.Modules.SalaryFields
{
    [System.Web.UI.ValidationProperty("ValidationText")]
    public partial class DropDownListSalaryFields : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindSalaryFields();
            }
        }

        protected override void OnDataBinding(EventArgs e)
        {
            BindSalaryFields();
            base.OnDataBinding(e);
        }
        public event EventHandler SelectedIndexChanged;
        protected void drd_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectedIndexChanged != null)
            {
                SelectedIndexChanged(sender, e);
            }
        }
        public bool AppendDataBoundItems
        {
            get
            {
                return drd.AppendDataBoundItems;
            }
            set
            {
                drd.AppendDataBoundItems = value;
            }
        }
        public string AccessKey
        {
            get
            {
                return drd.AccessKey;
            }
            set
            {
                drd.AccessKey = value;
            }
        }
        public System.Web.UI.AttributeCollection Attributes
        {
            get
            {
                return drd.Attributes;
            }
        }
        public bool AutoPostBack
        {
            get
            {
                return drd.AutoPostBack;
            }
            set
            {
                drd.AutoPostBack = value;
            }
        }
        public Color BackColor
        {
            get
            {
                return drd.BackColor;
            }
            set
            {
                drd.BackColor = value;
            }
        }
        public Color BorderColor
        {
            get
            {
                return drd.BorderColor;
            }
            set
            {
                drd.BorderColor = value;
            }
        }
        public System.Web.UI.WebControls.BorderStyle BorderStyle
        {
            get
            {
                return drd.BorderStyle;
            }
            set
            {
                drd.BorderStyle = value;
            }
        }
        public string CssClass
        {
            get
            {
                return drd.CssClass;
            }
            set
            {
                drd.CssClass = value;
            }
        }
        public string DataMember
        {
            get
            {
                return drd.DataMember;
            }
            set
            {
                drd.DataMember = value;
            }
        }
        public object DataSource
        {
            get
            {
                return drd.DataSource;
            }
            set
            {
                drd.DataSource = value;
            }
        }
        public string DataSourceID
        {
            get
            {
                return drd.DataSourceID;
            }
            set
            {
                drd.DataSourceID = value;
            }
        }
        public string DataTextField
        {
            get
            {
                return drd.DataTextField;
            }
            set
            {
                drd.DataTextField = value;
            }
        }
        public string DataTextFormatString
        {
            get
            {
                return drd.DataTextFormatString;
            }
            set
            {
                drd.DataTextFormatString = value;
            }
        }
        public string DataValueField
        {
            get
            {
                return drd.DataValueField;
            }
            set
            {
                drd.DataValueField = value;
            }
        }
        private bool _IsAddChooseItem = false;
        private bool _IsAddAllItem = false;
        private string _SelectedValue = "";
        public bool IsAddChooseItem
        {
            set
            {
                _IsAddChooseItem = value;
            }
            get
            {
                return _IsAddChooseItem;
            }
        }
        public bool IsAddAllItem
        {
            set
            {
                _IsAddAllItem = value;
            }
            get
            {
                return _IsAddAllItem;
            }
        }
        Dictionary<string, object> searchValues = new Dictionary<string, object>();
        public void AddSearchValues(string FieldName, object value)
        {
            searchValues.Add(FieldName, value);
        }
        private List<SalaryFieldsEntity> FilterList(List<SalaryFieldsEntity> ls)
        {
            if (ls == null) return new List<SalaryFieldsEntity>();
            if (searchValues.Count == 0) return ls;
            if (ls.Count == 0) return ls;
            System.Reflection.PropertyInfo[] fields = ls[0].GetType().GetProperties();
            return ls.FindAll(delegate(SalaryFieldsEntity oFind)
            {
                foreach (string key in searchValues.Keys)
                {
                    foreach (System.Reflection.PropertyInfo f in fields)
                    {
                        if (key.ToLower() == f.Name.ToLower())
                        {
                            if (f.GetValue(oFind, null).ToString().Equals(searchValues[key].ToString()))
                                return true;
                        }
                    }
                }
                return false;
            }
            );
        }
        //Bind data to txtSalaryFields
        public void BindSalaryFields()
        {
            SalaryFieldsController.LoadAllSalaryFields(false);
            List<SalaryFieldsEntity> lsSalaryFields = SalaryFieldsController.ListSalaryFieldsStatic;
            if (lsSalaryFields != null)
                lsSalaryFields = new List<SalaryFieldsEntity>(lsSalaryFields);
            lsSalaryFields = FilterList(lsSalaryFields);
            if (IsAddChooseItem)
            {
                SalaryFieldsEntity eChooseItem = new SalaryFieldsEntity();
                eChooseItem.FieldCode = Constants.CHOOSE_ITEM;
                lsSalaryFields.Insert(0, eChooseItem);
            }
            if (IsAddAllItem)
            {
                SalaryFieldsEntity eChooseItem = new SalaryFieldsEntity();
                eChooseItem.FieldCode = Constants.APP_ALL;
                lsSalaryFields.Insert(0, eChooseItem);
            }
            this.drd.DataTextField = "FieldCode";
            this.drd.SelectedValue = null;
            this.drd.DataValueField = "SalaryFieldId";
            this.drd.DataSource = lsSalaryFields;
            this.drd.DataBind();
            if (_SelectedValue == "")
            {
                if (this.drd.Items.Count > 0)
                {
                    this.drd.SelectedIndex = 0;
                }
            }
            else
            {
                if (drd.Items.FindByValue(_SelectedValue) != null)
                {
                    this.drd.SelectedValue = _SelectedValue;
                }
                else
                {
                    if (this.drd.Items.Count > 0)
                    {
                        this.drd.SelectedIndex = 0;
                    }
                }
            }
        }
        public string SelectedValue
        {
            get
            {
                return drd.SelectedValue;
            }
            set
            {
                _SelectedValue = value;
                ListItem item = drd.Items.FindByValue(value);
                if (item != null)
                    item.Selected = true;
            }
        }
        public int SelectedIndex
        {
            get
            {
                return drd.SelectedIndex;
            }
            set
            {
                drd.SelectedIndex = value;
            }
        }
        [System.ComponentModel.Browsable(false)]
        public string ValidationText
        {
            get
            {
                if (_IsAddChooseItem)
                {
                    if (drd.SelectedIndex <= 0)
                    {
                        return string.Empty;
                    }
                }
                else
                {
                    if (drd.SelectedIndex < 0) return string.Empty;
                }
                return drd.Text;
            }
        }
        public System.Web.UI.WebControls.Unit Width
        {
            get
            {
                return drd.Width;
            }
            set
            {
                drd.Width = value;
            }
        }
    }
}
